package com.chenqq.qsocket.adapter;

import com.chenqq.qsocket.handler.FileMessageHandler;
import com.chenqq.qsocket.handler.SocketMessageDispatcher;
import com.chenqq.qsocket.listener.SocketMessageListener;
import com.chenqq.qsocket.message.KeepAliveMessage;
import com.chenqq.qsocket.message.SocketMessage;
import com.chenqq.qsocket.message.TextMessage;
import com.chenqq.qsocket.tools.IoSessionManager;
import com.chenqq.qsocket.tools.LogShow;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import java.net.SocketAddress;

public  class TcpBasicAdapter extends IoHandlerAdapter {
    private String Tag = TcpBasicAdapter.class.getSimpleName();
    final SocketMessageDispatcher socketMessageDispatcher = new SocketMessageDispatcher();
    private SocketMessageListener socketMessageListener;

    public TcpBasicAdapter setTag(String tag) {
        Tag = tag;
        return this;
    }

    public TcpBasicAdapter setFileDown(String path,boolean isReceive){
        FileMessageHandler.setDownloadPath(path,isReceive);
        return this;
    }

    public TcpBasicAdapter setSocketMessageListener(SocketMessageListener socketMessageListener) {
        this.socketMessageListener = socketMessageListener;
        return this;
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        SocketAddress remoteAddress = session.getRemoteAddress();
        String replace = remoteAddress.toString().replace("/", "");
        String[] split = replace.split(":");
        super.sessionClosed(session);
        LogShow.showE("有客户端关闭"+remoteAddress.toString());
        IoSessionManager.getInStance().removeIoSession(Tag, split[0]);
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        super.sessionOpened(session);
        SocketAddress remoteAddress = session.getRemoteAddress();
        LogShow.showE("有客户端连接"+remoteAddress.toString());
        String replace = remoteAddress.toString().replace("/", "");
        String[] split = replace.split(":");
        IoSessionManager.getInStance().addIoSession(Tag, split[0], session);
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {

        super.messageSent(session, message);
        LogShow.showE("有客户端发送"+session.getRemoteAddress().toString());
        if (socketMessageListener!=null){
            socketMessageListener.sendMessageSuccess(Tag);
        }
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {

        super.messageReceived(session, message);
        //还有文字//HEX十六进制字符串
        SocketAddress remoteAddress = session.getRemoteAddress();
        String replace = remoteAddress.toString().replace("/", "");
        String[] split = replace.split(":");
        if (message instanceof SocketMessage) {
            socketMessageDispatcher.handleMessage(session, (SocketMessage) message,
                    Tag,socketMessageListener);
        } else if (message instanceof String) {
            //处理文字HeX
            if (socketMessageListener!=null){
                socketMessageListener.receiveTxtSuccess(split[0], (String) message);
            }
        }
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
//        super.exceptionCaught(session, cause);
        SocketAddress remoteAddress = session.getRemoteAddress();
        String replace = remoteAddress.toString().replace("/", "");
        String[] split = replace.split(":");
        super.exceptionCaught(session, cause);
        LogShow.showE("客户端异常"+remoteAddress+"  ="+cause.getMessage());
        IoSessionManager.getInStance().removeIoSession(Tag, split[0]);
    }
}
